/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui.db;

import blueprint4j.Constants;
import blueprint4j.db.*;
import blueprint4j.gui.*;
import blueprint4j.report.*;
import blueprint4j.utils.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import javax.swing.*;
import java.util.Hashtable;

public abstract class DataPanelReport extends DataPanel implements ActionListener {
	private String report_name[] = null, resource[] = null;
	private Hashtable extra_properties[] = null;
	private JButton report_btn_show[] = null, report_btn_print[] = null;
	private DBConnection connection = null;
	private VectorBindable bindables = new VectorBindable();
	private boolean print = false;

	public DataPanelReport(DBConnection connection,String report_name[],String resource[],Hashtable[] extra_properties,boolean print) {
		super();
		this.report_name = report_name;
		this.resource =resource;
		this.extra_properties = extra_properties;
		this.connection = connection;
		this.print = print;
		report_btn_show = new JButton[report_name.length];
		report_btn_print = new JButton[report_name.length];
		for (int i =0;i < report_btn_show.length;i++) {
			report_btn_show[i] = new JButton(report_name[i]);
			report_btn_show[i].addActionListener(this);
			if (print) {
				report_btn_print[i] = new JButton("Print " + report_name[i]);
				report_btn_print[i].addActionListener(this);
			}
		}
	}
	
	public DataPanelReport(DBConnection connection,String report_name[],String resource[],boolean print) {
		this(connection,report_name,resource,null,print);
	}
	
	public abstract String getName();

	public void setBindables(VectorBindable bindables) {
		this.bindables = bindables;
	}

	public void build(Binder binder, Bindable entity)
	throws IOException, BindException {
		try {
			DataGroup information = new DataGroup("Reports",Constants.ICON_BASE+"actions/frameprint.png");
			for (int i =0;i < report_name.length;i++) {
				information.add(report_btn_show[i]);
			}
			addGroup(information);
			if (print) {
				information = new DataGroup("Print Reports",Constants.ICON_BASE+"actions/agt_print.png");
				for (int i =0;i < report_name.length;i++) {
					information.add(report_btn_print[i]);
				}
				addGroup(information);
			}
		} catch (Exception ee) {
			throw new BindException(ee);
		}
	}

	public void actionPerformed(ActionEvent event) {
		try {
//			for (int i =0;i < report_btn_show.length;i++) {
//				if (event.getSource() == report_btn_show[i]) {
//					Report report = null;
//					if (extra_properties != null) {
//						report = new Report(connection,resource[i],bindables,extra_properties[i]);
//					} else {
//						report = new Report(connection,resource[i],bindables);
//					}
//					report.build();
//					JFrame frame = report.getComponent();
//					frame.show();
//				}
//			}
//			for (int i =0;i < report_btn_print.length;i++) {
//				if (event.getSource() == report_btn_print[i]) {
//					Report report = null;
//					if (extra_properties != null) {
//						report = new Report(connection,resource[i],bindables,extra_properties[i]);
//					} else {
//						report = new Report(connection,resource[i],bindables);
//					}
//					report.build();
//					report.print();
//				}
	//		}
		} catch (Exception exception) {
			Log.debug.out(exception);
		}
	}

	public void activated() {}

	public void deactivated() {}
}

